class GameUtil{
    //获取两点之间的角度
    public static get2PointAngle(point1:egret.Point, point2:egret.Point):number
    {
        let deltx = point1.x - point2.x;
        let delty = point1.y - point2.y;
        let hypotenuse = Math.sqrt(Math.pow(deltx, 2)+Math.pow(delty, 2)); 
        //斜边长度 
        let cos = deltx/hypotenuse; 
        let radian = Math.acos(cos); 
        //求出弧度 
        let angle = 180/(Math.PI/radian); 
        //用弧度算出角度 
        if (delty<0) { 
            angle = -angle; 
        }
        else if ((delty == 0) && (deltx < 0)) { 
            angle = 180; 
        } 
        return angle; 
    }

    //带权重的随机数算法
    //格式：1_2_3_4_5|0.7#6_7|0.3
    /**
     * 下划线分割等概率id,竖线分割当前权重，#分割下个权重所对应的id组
     */
    public static RandomByWeight(weigtString:string)
    {
        let arr = [];
        arr = weigtString.split('#');
        let aimProp:number = Math.random();
        let weightArr = new Dictionary<any>();

        for(let v of arr)
        {
            let list = v.split('|');
            let Weight = Number(list[1]);
            let value = list[0];
            weightArr.add(Weight, value);
        }

        let startWeight = 0;
        for(let v of weightArr.keys)
        {
            let nextWeight = startWeight + v;
            if(aimProp >= startWeight && aimProp <=nextWeight)
            {
                let value = weightArr.getValueByKey(v);
                let valueArr = value.split('_');
                let index : number = Math.random() * valueArr.length << 0;
                let aimID:number = parseInt(valueArr[index]);
                return aimID;
            }
            else
            {
                startWeight = v;
            }
        }
        return -1;
    }

     //派发事件
	public static dispatchEvent(type:string, obj:Object = null, bubbles:boolean = false, cancelable:boolean = false):void
	{ 	
		var event = new BaseEvent(type,obj,bubbles,cancelable);
		EventManager.getInstance().dispatchEvent(event);
	}

	//监听事件
	public static addEventListener(type:string,listener:Function,thisObject:any=null,useCapture:boolean=false,priority:number=0):void
	{ 
		EventManager.getInstance().addEventListener(type,listener,thisObject,useCapture,priority);
	}

    public static removeEventListener(type:string,listener:Function,thisObject:any=null,useCapture:boolean=false,priority:number=0):void
	{ 
		EventManager.getInstance().removeEventListener(type,listener,thisObject,useCapture);
	}

    /**检测碰撞*/
    public static hitRect(obj1:egret.DisplayObject,obj2):boolean
    {
        var rect1: egret.Rectangle = obj1.getBounds();

        rect1.x = obj1.x;
        rect1.y = obj1.y;
        return rect1.intersects(obj2);
    }

    /**
     * 比较微信基础库版本
     * 返回结果 0表示版本相同
     * 1表示 V1比V2新
     * -1表示 V1比V2老
    */
    public static compareVersion(v1, v2):number{
        v1 = v1.split('.');
        v2 = v2.split('.');
        let len = Math.max(v1.length, v2.length);
        
        while (v1.length < len) {
            v1.push('0');
        }
        while (v2.length < len) {
            v2.push('0');
        }
        
        for (let i = 0; i < len; i++) {
            let num1 = parseInt(v1[i]);
            let num2 = parseInt(v2[i]);
        
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
        }
        return 0;
    }

    	//缩短昵称,超过12个字符，都缩短
	public static getNameSlice(nameStr) {
		let tempName = nameStr;
		if (GameUtil.strlen(nameStr) > 12) {
			tempName = nameStr.substring(0, 6) + "...";
		}
		return tempName;
	}

	//获取字符串长度(英文占1个字符，中文汉字占2个字符)
	public static strlen(str) {
		var len = 0;
		for (var i = 0; i < str.length; i++) {
			var c = str.charCodeAt(i);
			//单字节加1   
			if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			len++;
			}
			else {
			len += 2;
			}
		}
		return len;
	}

}